﻿using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UIWindows.GlobalStatics;
using UIWindows.Models.Interact;
using System.Collections.ObjectModel;
using UIWindows.Enums;

namespace UIWindows.ViewModels.Dialogs
{
    public class NewPLCResectionViewDialog : Domains.ViewModelBase
    {
        #region 属性
        private System.Windows.Visibility _sUpdate;

        public System.Windows.Visibility IsUpdate
        {
            get { return _sUpdate; }
            set { SetProperty(ref _sUpdate, value); }
        }
        public ObservableCollection<string> ServiceList { get; set; }

        public ObservableCollection<int> IndexList { get; set; }
        private string _ServiceName;
        /// <summary>
        /// 服务名
        /// </summary>
        public string ServiceName
        {
            get { return _ServiceName; }
            set {
                IndexList.Clear();
                if (value != null) {
                    var model = _configStatic.SignalList.FirstOrDefault(x => x.Name == value);
                    if (model != null)
                    {
                        for (int i = 1; i < (model.Length_Resection + 1); i++)
                        {
                            if (_configStatic.PLCResectionList.Any(x => x.ServiceName == value && x.Index == i))
                            {
                                continue;
                            }
                            IndexList.Add(i);
                        }
                    }
                }
                _ServiceName = value;
                PRM.ServiceName = value;
            }
        }

        #endregion

        public PLCResectionModel PRM { get; set; }

        private bool _isUpdate;
        private SystemConfigStatic _configStatic;
        public NewPLCResectionViewDialog(SystemConfigStatic configStatic, PLCResectionModel model, bool isUpdate = false)
        {
            _configStatic = configStatic;
            _isUpdate = isUpdate;
            IndexList = new ObservableCollection<int>();
            ServiceList = new ObservableCollection<string>();
            foreach (var item in configStatic.SignalList)
            {
                ServiceList.Add(item.Name);
            }
            if (isUpdate)
            {
                PRM = model;
                IsUpdate = System.Windows.Visibility.Visible;
                ServiceName = PRM.ServiceName;
            }
            else
            {
                PRM = new PLCResectionModel();
                IsUpdate = System.Windows.Visibility.Collapsed;
            }
        }

        public Commands.DelegateCommand Confirm => new Commands.DelegateCommand(() =>
        {
            if (PRM.Index == 0)
            {
                if (DialogHost.IsDialogOpen(PopUpTypes.Global.ToString())) { DialogHost.Close(PopUpTypes.Global.ToString()); }
                return;
            }
            if (!_isUpdate)
            {
                _configStatic.PLCResectionList.Add(new PLCResectionModel()
                {
                    ServiceName = _ServiceName,
                    Index = PRM.Index,
                    Name = PRM.Name,
                });
            }
            _configStatic.Save();
            if (DialogHost.IsDialogOpen(PopUpTypes.Global.ToString())) { DialogHost.Close(PopUpTypes.Global.ToString()); }
        });

        public Commands.DelegateCommand Delete => new Commands.DelegateCommand(() =>
        {
            _configStatic.PLCResectionList.Remove(PRM);
            _configStatic.Save();
            if (DialogHost.IsDialogOpen(PopUpTypes.Global.ToString())) { DialogHost.Close(PopUpTypes.Global.ToString()); }
        });
    }
}
